import os

from detect.src.forms.uploadForm import uploadForm
from django.http import HttpResponse, request, JsonResponse
from django.shortcuts import render, redirect
from detect.models import UserImage
from django.conf import settings
from detect.src.detect2file import DetectDemo
def ImgInference(req):
    user = req.GET.get('user')
    img = UserImage.objects.get(UserId=user)
    imagepath = os.path.join(settings.MEDIA_ROOT, img.Images.name)
    from detectron2.data.detection_utils import read_image
    img = read_image(imagepath,format="BGR")
    prediction = DetectDemo.DetectDemo()
    result = prediction.detection(img)
    result.save(os.path.join(settings.IMAGE_ROOT,'processing.png'))
    processing_url = settings.IMAGE_URL + f'/processing.png'
    return JsonResponse({'processing_url':processing_url})


